package com.sunye.elasticsearch.service.impl;


import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sunye.elasticsearch.entity.EsDog;
import com.sunye.elasticsearch.entity.EsDogIndex;
import com.sunye.elasticsearch.entity.EsDogSellingPointIndex;
import com.sunye.elasticsearch.repository.EsDogIndexRepository;
import com.sunye.elasticsearch.service.BaseElasticsearchService;
import com.sunye.elasticsearch.service.EsDogIndexService;
import com.sunye.elasticsearch.service.EsDogSellingPointService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.geo.GeoPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 商品索引业务层实现
 *
 * @author paulG
 * @since 2020/10/14
 **/
@Slf4j
@Service
public class EsDogIndexServiceImpl extends BaseElasticsearchService implements EsDogIndexService {

    @Autowired
    private ElasticsearchOperations restTemplate;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private EsDogServiceImpl esDogService;

    @Autowired
    private EsDogSellingPointService esDogSellingPointService;

    @Autowired
    private EsDogIndexRepository esDogIndexRepository;

    @Override
    public void init() {
        String indexName = "dog_index";
        //如果索引存在，则删除，重新生成。
        if (this.indexExist(indexName)) {
            deleteIndexRequest(indexName);
        }
        //创建索引 (创建索引后使用GET /dog_index/_mapping/查看index的mapping结构)
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(EsDogIndex.class);
        boolean res = indexOperations.create();
        if (res) {
            Document mapping = indexOperations.createMapping();
            indexOperations.putMapping(mapping);
        }
        //初始化数据
        QueryWrapper<EsDog> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted","0");
        List<EsDog> list = esDogService.list(queryWrapper);

//        ThreadUtil.execAsync(() -> {
            try {
                List<EsDogIndex> esDogIndexList = new ArrayList<>();
                for (EsDog esDog : list) {
                    EsDogIndex esDogIndex = new EsDogIndex(esDog);
                    String[] sellingPoint = esDog.getSellingPointId().split(",");
                    List<String> sellingPointIds = Arrays.asList(sellingPoint);
                    //存储卖点
                    List<EsDogSellingPointIndex> esDogSellingPointList =  esDogSellingPointService.getSellingPoint(sellingPointIds);
                    esDogIndex.setSellingPointList(esDogSellingPointList);
                    //存储经纬度
                    GeoPoint geoPoint = new GeoPoint();
                    geoPoint.reset(esDog.getLat(),esDog.getLon());
                    esDogIndex.setLocation(geoPoint);
                    //存储sku
                    List<EsDogIndex.Sku> skuList = new ArrayList<>();
                    EsDogIndex.Sku sku = new EsDogIndex.Sku();
                    sku.setId(esDog.getId());
                    sku.setTitle(esDog.getDogBreed());
                    skuList.add(sku);
                    esDogIndex.setSkuList(skuList);
                    esDogIndexList.add(esDogIndex);
                }
                //将输入存入到es里
                if (!esDogIndexList.isEmpty()) {
                    esDogIndexRepository.deleteAll();
                    Iterable<EsDogIndex> esDogIndices = esDogIndexRepository.saveAll(esDogIndexList);
                    System.out.println("esDogIndices = " + esDogIndices);
                }

            } catch (Exception e) {
                log.error("商品索引生成异常：", e);
            }
    }

}
